//
//  TWTopicViewController.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/3/29.
//  Copyright © 2017年 个人. All rights reserved.
//  父类：增加上下拉刷新功能

import UIKit


class TWTopicViewController: UITableViewController {

    // MARK:- 懒加载
    fileprivate lazy var headerView : TWFooterView = TWFooterView.createFooterView()        // 下拉刷新
    lazy var footerView : TWFooterView = TWFooterView.createFooterView()                    // 上拉加载更多
    lazy var itemsArray : NSMutableArray = NSMutableArray()                                 // 存放数据的字典数组
    lazy var footerNullView : UILabel = UILabel()                               // 空的脚部视图
    
    // MARK:- 属性
    var titleModel : TWDiscoverTitleModel?
    var isHeaderRefreshing : Bool = false
    var isFooterRefreshing : Bool = false
    
    // MARK:- 系统回调
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        tableView.contentInset = UIEdgeInsets(top: NavigationBarHeight + DiscovertitleViewHeight, left: 0, bottom: TabBarHeight, right: 0)
        setupRefreshView()
        headerBeginRefreshing()
    }
}

// MARK:- UI界面
extension TWTopicViewController {
    fileprivate func setupRefreshView(){
        headerView.frame = CGRect(x: 0, y: -DiscovertitleViewHeight, width: UIScreen.main.bounds.width, height: 20)
        headerView.alpha = 0
        tableView.addSubview(headerView)
        footerView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 20)
        tableView.tableFooterView = footerView
        // 没有数据时的tableFooterView
        footerNullView = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
        footerNullView.text = "- end -"
        footerNullView.font = UIFont.init(name: "Lobster1.4", size: 15)
        footerNullView.textAlignment = .center
    }
}

// MARK:- ----代理方法----
extension TWTopicViewController {
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let insetTop = NavigationBarHeight + DiscovertitleViewHeight
        // 滚动偏移量
        let offset = -(tableView.contentOffset.y + insetTop)  // 默认为0
        dealHeader(offset: offset)
        dealFooter(offset: offset)
    }
    
    // 用户松开scrollView时调用
    override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        let insetTop = NavigationBarHeight + DiscovertitleViewHeight
        // 滚动偏移量
        let offset = -(tableView.contentOffset.y + insetTop)
        if offset >= 40 {
            headerBeginRefreshing()
        }
    }
}

// MARK:- 自定义方法
extension TWTopicViewController {
    fileprivate func dealHeader(offset : CGFloat){
        if isHeaderRefreshing {
            return
        }
        if offset > 0 {
            // 向下拉
            headerView.alpha = offset / 40
        }
    }
    
    fileprivate func dealFooter(offset : CGFloat){
        // 还没有任何内容的时候，不需要判断
        if tableView.contentSize.height == 0 {
            return
        }
        if isFooterRefreshing {
            return
        }
        // 计算偏移量 = 内容的顶部和frame的顶部的差值,tableView.contentSize包括header和footer
        let ofsetY = tableView.contentSize.height + tableView.contentInset.bottom - tableView.frame.height
        if tableView.contentOffset.y >= ofsetY {
            footerBeginRefresh()
        }
    }
    // MARK:- 刷新相关
    func footerBeginRefresh() {
        if isFooterRefreshing {
            return
        }
        // 进入刷新状态
        isFooterRefreshing = true
        footerView.addRotationAnimation()
        loadMoreData()
    }
    func footerEndRefresh() {
        isFooterRefreshing = false
        self.footerView.loading_in_ImageView.layer.removeAllAnimations()
    }
    
    func headerBeginRefreshing(){
        if isHeaderRefreshing {
            return
        }
        isHeaderRefreshing = true
        headerView.addRotationAnimation()
        UIView.animate(withDuration: 0.25, animations: {
            var inset : UIEdgeInsets = self.tableView.contentInset
            inset.top += 40
            self.tableView.contentInset = inset
            // self.tableView.contentOffset = CGPoint(x: self.tableView.contentOffset.x, y: -inset.top)
        })
        loadNewData()
    }
    func headerEndRefreshing(){
        isHeaderRefreshing = false
        UIView.animate(withDuration: 0.25, animations: {
            var inset : UIEdgeInsets = self.tableView.contentInset
            inset.top -= 40
            self.tableView.contentInset = inset
            self.headerView.alpha = 0
        }) { (_) in
            self.headerView.loading_in_ImageView.layer.removeAllAnimations()
        }
    }
}

// MARK:- 子类重写方法
extension TWTopicViewController {
    func loadNewData() {
        // 子类去重写
    }
    func loadMoreData() {
        // 子类去重写
    }
}
